package cn.pku.ss.sirx.sna.ib;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import cn.pku.ss.sirx.sna.ib.bean.Influence;
import cn.pku.ss.sirx.sna.ib.util.Util;

/**
 * A container of influence.
 * When an Influence instance is added into the Container,
 * if there's an existing influence with the same influencer, influencee and time,
 * their values will be merged.
 *
 * @author WangXiao
 * @version 1.0
 */
public class InfluenceContainer {

    /**
     * Add a new influence into the container.
     * @param influence a new influence to add
     */
    public void add(Influence influence) {
        this.prepropcess(influence);
        Influence old = this.influenceMap.get(influence);
        if (old != null) {
            old.setValue(old.getValue() + influence.getValue());
        }
        else {
            this.influenceMap.put(influence, influence);
        }
    }

    /**
     * Get all influences in this container.
     * @return all influences in this container
     */
    public Collection<Influence> getCollection() {
        return this.influenceMap.values();
    }
    
    //////////////////////////////////////////////////////////////////////////////
    
    private void prepropcess(Influence influence) {
        influence.setDate(Util.filterTime(influence.getDate()));
    }

    //////////////////////////////////////////////////////////////////////////////

    private Map<Influence, Influence> influenceMap = new HashMap<Influence, Influence>();
    
}
